package com.atguigu.gmall.publisher.controller;


import com.atguigu.gmall.publisher.service.SugarService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * @author hjy
 * @create 2023/3/22 15:25
 */
@RestController
@RequestMapping("/sugar")
public class Controller {

    @Autowired
    private SugarService sugarService;
    @RequestMapping("/text")
    public String text(){
        System.out.println("aaaa");
        return "success";
    }

    @RequestMapping("/Gmv")
    public String Gmv(@RequestParam(value = "date",defaultValue = "0") int date){
        if (date==0){
            date=getToday();
        }
        //查询GMV

        BigDecimal GMV = sugarService.getGMv(date);


        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": "+GMV+"\n" +
                "}";
    }
    @RequestMapping("/topN")
    public String getTopN(@RequestParam(value = "date",defaultValue = "0") int date,@RequestParam(value = "limit",defaultValue = "5") int limit){
        if (date==0){
            date=getToday();
        }
        Map<String, BigDecimal> topN = sugarService.getTopN(date, limit);
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [\"" +
                 StringUtils.join(topN.keySet(),"\",\"") +
                " \"],\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"品牌\",\n" +
                "        \"data\": [" +
                StringUtils.join(topN.values(),",") +

                "]\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }

    private int getToday() {
        SimpleDateFormat toYyyyMMdd = new SimpleDateFormat("yyyyMMdd");
        String format = toYyyyMMdd.format(System.currentTimeMillis());
        return Integer.parseInt(format);
    }

}
